﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com
using System;
using System.Threading;
using BrainTechLLC.ImageOperators;
using System.Diagnostics;
using System.Drawing;
using BrainTechLLC;
using BrainTechLLC.WPF;
using BrainTechLLC.ThreadSafeObjects;
using System.Windows;
using BrainTechLLC.ImageOperatorsNonAForgeDependent;
using System.IO;
using System.Drawing.Imaging;

namespace ImageAppDataModels
{
    [Serializable]
    public partial class SingleThumbnail : BaseNotifyPropertyChangedUI
    {
        public int _thumbWidth;
        public int _thumbHeight;
        public PixelFormat _thumbPixelFormat;
        public IHasBitmap HasBitmap;
        public ThumbnailProvider ThumbnailProvider;
        protected Bitmap _thumbnail;
        public string FileName;
        
        [NonSerialized]
        protected FastSmartWeakEvent<EventHandler> _thumbnailSet = new FastSmartWeakEvent<EventHandler>();
        public event EventHandler ThumbnailSet { add { _thumbnailSet.Add(value); } remove { _thumbnailSet.Remove(value); } }

        [NonSerialized]
        protected FastSmartWeakEvent<EventHandler<ObjectEventArgs<IHasBitmap, ResizeInfo>>> _rotateResizeDone = new FastSmartWeakEvent<EventHandler<ObjectEventArgs<IHasBitmap, ResizeInfo>>>();
        public event EventHandler<ObjectEventArgs<IHasBitmap, ResizeInfo>> RotateOrResizeDone { add { _rotateResizeDone.Add(value); } remove { _rotateResizeDone.Remove(value); } }

        public void OnRotateOrResizeDone(IHasBitmap IHasBitmap, ResizeInfo resizeInfo)
        {
            _rotateResizeDone.Raise(this, new ObjectEventArgs<IHasBitmap, ResizeInfo>(IHasBitmap, resizeInfo));
        }        
        
        public void ClearThumbnailSilently()
        {
            if (_thumbnail != null)
            {
                int num = ThumbnailProvider.ThumbsInMemory.Decrement();
                Debug.Assert(num >= 0);
                _thumbnail.Dispose();
            }
            _thumbnail = null;
        }
        
        public Bitmap Thumbnail
        {
            get
            {
                if (_thumbnail == null)
                {
                    _thumbnail = ThumbnailProvider.CreateAndSetThumbnail(HasBitmap, FileName);
                }

                return _thumbnail;
            }
            set
            {
                HandleChangeThumbnailValue(value, true);
            }
        }
        
        public Bitmap ThumbnailCached
        {
            get { return _thumbnail; }
            set { HandleChangeThumbnailValue(value, false); }
        }

        public void SetThumbnail(Bitmap thumbnail)
        {
            Thumbnail = thumbnail;
        }

        public void HandleChangeThumbnailValue(Bitmap newThumb, bool raiseOnThumbnailSet)
        {
            if (_thumbnail != newThumb)
            {
                if (_thumbnail != null)
                {
                    int num = ThumbnailProvider.ThumbsInMemory.Decrement();
                    Debug.Assert(num >= 0);
                    _thumbnail.Dispose();
                }
                
                _thumbnail = newThumb;

                if (newThumb != null)
                {
                    _thumbWidth = newThumb.Width;
                    _thumbHeight = newThumb.Height;
                    _thumbPixelFormat = newThumb.PixelFormat;
                    ThumbnailProvider.ThumbsInMemory.Increment();
                }

                if (raiseOnThumbnailSet)
                {
                    OnThumbnailSet();
                }

                OnPropertyChanged("Thumbnail");
            }
        }

        public void SetFilteredThumbnail(Bitmap bm)
        {
            HandleChangeThumbnailValue(bm, false);
        }

        public void OnThumbnailSet()
        {
            HasBitmap.OnThumbnailSet(this);
            _thumbnailSet.Raise(this, EventArgs.Empty);
        }        

        public long GetByteEstimateForThumbnailCached()
        {
            return ThumbnailCached == null ? 0 : ThumbnailCached.GetByteEstimate(this._thumbWidth, this._thumbHeight, this._thumbPixelFormat);
        }
    }
}

//private void GetThumbnailOneWay()
//{
//    if (_thumbnail != null && !_thumbnailDirty)
//        return;
//    Bitmap bm = _bitmap;
//    if (bm != null && !_bitmapDirty)
//    {
//        Thumbnail = HandleLoadThumbnail(bm);
//    }
//    else
//    {
//        Thumbnail = HandleLoadThumbnail(ReadBitmapFromByteStream(EnsureImageBytesLoaded()));
//    }
//}
